﻿
namespace TFSChangeLog.DataAccess
{
    using System;
    using Microsoft.TeamFoundation.Client;
    using Microsoft.TeamFoundation.VersionControl.Client;

    /// <summary>Summary description for <see cref="TFSConnection"/> class.</summary>
    public static class TFSConnection
    {
        /// <summary>Summary description for class level field of type <c>Microsoft.TeamFoundation.Client.TfsTeamProjectCollection</c>.</summary>
        private static TfsTeamProjectCollection tfs = null;

        /// <summary>Gets or sets the value for tfsServerName.</summary>
        public static string tfsServerName { get; set; }

        /// <summary>Gets or sets the value for selectedProjectName.</summary>
        public static string selectedProjectName { get; set; }

        /// <summary>Summary description for <c>TFSConnection</c> method.</summary>
        /// <param name="TFSServerName">TODO: Summary description for <c>TFSServerName</c> parameter of type <c>string</c>.</param>
        /// <param name="SelectedProjectName">TODO: Summary description for <c>SelectedProjectName</c> parameter of type <c>string</c>.</param>
        public static void ConnectToTFS(string TFSServerName, string SelectedProjectName)
        {
            try
            {
                var projectCollectionURI = new Uri(TFSServerName);

                tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(projectCollectionURI);
                tfs.EnsureAuthenticated();
                tfsServerName = TFSServerName;
                selectedProjectName = SelectedProjectName;
            }
            catch (Exception e)
            {
                throw new ApplicationException("Error encountered while connecting to TFS Server." + e.Message);
            }
        }

        /// <summary>Summary description for <c>TFSConnection</c> method.</summary>
        /// <param name="tfsProjectname">TODO: Summary description for <c>tfsProjectname</c> parameter of type <c>string</c>.</param>
        /// <returns>TODO: returns an object of type <c>bool</c>.</returns>
        public static bool CheckIfProjectExist(string tfsProjectname)
        {
            if (tfs == null)
                {
                    return false;
                }

            var vcs = tfs.GetService<VersionControlServer>();
            var tp = vcs.TryGetTeamProject(tfsProjectname);

            if (tp != null)
                {
                    return true;
                }

            return false;
        }

        /// <summary>Summary description for <c>TFSConnection</c> method.</summary>
        /// <param name="TFSServerName">TODO: Summary description for <c>TFSServerName</c> parameter of type <c>string</c>.</param>
        /// <param name="SelectedProjectName">TODO: Summary description for <c>SelectedProjectName</c> parameter of type <c>string</c>.</param>
        /// <param name="tfsTeamProjectCollection">TODO: Summary description for <c>tfsTeamProjectCollection</c> parameter of type <c>Microsoft.TeamFoundation.Client.TfsTeamProjectCollection</c>.</param>
        public static void ConnectToTFS(string TFSServerName, string SelectedProjectName, out TfsTeamProjectCollection tfsTeamProjectCollection)
        {
            tfsTeamProjectCollection = null;
            try
            {
                var projectCollectionURI = new Uri(TFSServerName);

                tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(projectCollectionURI);
                tfsServerName = TFSServerName;
                selectedProjectName = SelectedProjectName;
                tfs.EnsureAuthenticated();
                tfsTeamProjectCollection = tfs;
            }
            catch (Exception e)
            {
                throw new ApplicationException("Error encountered while connecting to TFS Server." + e.Message);
            }
        }

        /// <summary>Summary description for <c>TFSConnection</c> method.</summary>
        public static void DisconnectToTFS()
        {
            tfs.Dispose();
        }

        /// <summary>Summary description for <c>TFSConnection</c> method.</summary>
        /// <returns>TODO: returns an object of type <c>Microsoft.TeamFoundation.Client.TfsTeamProjectCollection</c>.</returns>
        public static TfsTeamProjectCollection GetTFSConnection()
        {
            return tfs ?? null;
        }

        /// <summary>Summary description for <c>TFSConnection</c> method.</summary>
        /// <returns>TODO: returns an object of type <c>string</c>.</returns>
        public static string GetTFSServerURL()
        {
            return tfsServerName ?? null;
        }

        /// <summary>Summary description for <c>TFSConnection</c> method.</summary>
        /// <returns>TODO: returns an object of type <c>string</c>.</returns>
        public static string GetTFSSelectedProjectName()
        {
            return selectedProjectName ?? null;
        }
    }
}
